package com.example.company.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.company.databinding.ItemCompanyBinding
import com.example.company.entity.CompanyEntity

class CompanyAdapter(private val data:List<CompanyEntity>):RecyclerView.Adapter<CompanyAdapter.CompanyViewHolder>() {
    inner class CompanyViewHolder(val itemDataBinding:ItemCompanyBinding):RecyclerView.ViewHolder(itemDataBinding.root)

    private var itemOnClickListener:ItemOnClickListener? = null

    fun setItemOnClickListener(listener:ItemOnClickListener){
        this.itemOnClickListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CompanyViewHolder {
        return CompanyViewHolder(ItemCompanyBinding.inflate(LayoutInflater.from(parent.context),parent,false))
    }

    override fun getItemCount(): Int = data.size

    override fun onBindViewHolder(holder: CompanyViewHolder, position: Int) {
        val entity = data[position]
        holder.itemDataBinding.entity = entity
        holder.itemDataBinding.executePendingBindings()
        holder.itemDataBinding.root.setOnClickListener {
            itemOnClickListener?.onClick(it,position)
        }
    }
    interface ItemOnClickListener{
        fun onClick(view:View,position: Int)
    }
}